<?php

namespace App\Models;

use TopClient;
use AlibabaAliqinFcSmsNumSendRequest;
use AlibabaAliqinFcTtsNumSinglecallRequest;
use Illuminate\Support\Facades\Redis;
use App\Utils\ChuangLanVoiceCode;
use App\Utils\ChuangLanVoiceCodeByV2Version;
use Exception;
use Log;

class SmsMessage
{
    //发送验证码
    //type属性：1用户端，2商户端，3后台, 4地推商家，5商家后台
    //mode: 1短信验证码，2语音验证码
    //0发送失败，1发送成功，2等待60秒
    public static function loginIdentifyingCode($mobile,$type,$mode = '1') {
        try {
            if ($type == '1') {
                $codettl = Redis::ttl('User:SmsMobile:' . $mobile);
                if ($codettl) {
                    if ($codettl > 540) {
                        return '2'; //0发送失败，1发送成功，2等待60秒
                    }
                }
            } elseif ($type == '2') {
                $codettl = Redis::ttl('Shop:SmsMobile:' . $mobile);
                if ($codettl) {
                    if ($codettl > 540) {
                        return '2'; //0发送失败，1发送成功，2等待60秒
                    }
                }
            } elseif ($type == '3') {
                $codettl = Redis::ttl('Admin:SmsMobile:' . $mobile);
                if ($codettl) {
                    if ($codettl > 540) {
                        return '2'; //0发送失败，1发送成功，2等待60秒
                    }
                }
            } elseif ($type == '4') {
                $codettl = Redis::ttl('Shop:SalesMan:SmsMobile:' . $mobile);
                if ($codettl) {
                    if ($codettl > 540) {
                        return '2'; //0发送失败，1发送成功，2等待60秒
                    }
                }
//                if ($mode != '2') {
//                    return '0';
//                }
            } elseif ($type == '5') {
                $codettl = Redis::ttl('ShopWeb:SmsMobile:' . $mobile);
                if ($codettl) {
                    if ($codettl > 540) {
                        return '2'; //0发送失败，1发送成功，2等待60秒
                    }
                }
            }

            $code = '';
            for ($i = 1; $i <= 6; $i++) {
                $code = $code . rand(0, 9);
            }
            $c = new TopClient;
            $c->appkey = env('ALIDAYU_APPKEY');
            $c->secretKey = env('ALIDAYU_SECRETKEY');
            if ($mode == '1') {
                $req = new AlibabaAliqinFcSmsNumSendRequest;
                $req->setExtend("123456");
                $req->setSmsType("normal");
                $req->setSmsFreeSignName("翻贝网");
                $req->setSmsParam("{\"code\":\"" . $code . "\",\"product\":\"翻贝网\"}");
                $req->setRecNum($mobile);
                $req->setSmsTemplateCode("SMS_12705225");
                $resp = $c->execute($req);
            } elseif ($mode == '2') {
                $smscode = '';
                for ($i = 0; $i < 6; $i++) {
                    $smscode = $smscode . '，' . substr($code, $i, 1);
                }
                //阿里大于语音
//            $req = new AlibabaAliqinFcTtsNumSinglecallRequest;
//            $req->setExtend("123456");
//            $req->setTtsParam("{\"code\":\"".$smscode."\",\"product\":\"翻贝网\"}");
//            $req->setCalledNum($mobile);
//            $req->setCalledShowNum("01053912808");
//            $req->setTtsCode("TTS_34305283");
//            $resp = $c->execute($req);

                //创蓝语音
                $chuangLanVoiceCode = new ChuangLanVoiceCodeByV2Version();
                $result = $chuangLanVoiceCode->sendVoiceCode($mobile, $smscode);

                if ($type == '1') {
                    Redis::setex('User:SmsMobile:' . $mobile, 600, $code);
                } elseif ($type == '2') {
                    Redis::setex('Shop:SmsMobile:' . $mobile, 600, $code);
                } elseif ($type == '3') {
                    Redis::setex('Admin:SmsMobile:' . $mobile, 600, $code);
                } elseif ($type == '4') {
                    Redis::setex('Shop:SalesMan:SmsMobile:' . $mobile, 600, $code);
                } elseif ($type == '5') {
                    Redis::setex('ShopWeb:SmsMobile:' . $mobile, 600, $code);
                }
                return '1';
            } else {
                return '0';
            }
            if ($resp->code) {
                if ($type == '1') {
                    Redis::setex('User:SmsMobile:' . $mobile, 600, $code);
                } elseif ($type == '2') {
                    Redis::setex('Shop:SmsMobile:' . $mobile, 600, $code);
                } elseif ($type == '3') {
                    Redis::setex('Admin:SmsMobile:' . $mobile, 600, $code);
                } elseif ($type == '4') {
                    Redis::setex('Shop:SalesMan:SmsMobile:' . $mobile, 600, $code);
                } elseif ($type == '5') {
                    Redis::setex('ShopWeb:SmsMobile:' . $mobile, 600, $code);
                }
                return '0';
            } else {
                if ($type == '1') {
                    Redis::setex('User:SmsMobile:' . $mobile, 600, $code);
                } elseif ($type == '2') {
                    Redis::setex('Shop:SmsMobile:' . $mobile, 600, $code);
                } elseif ($type == '3') {
                    Redis::setex('Admin:SmsMobile:' . $mobile, 600, $code);
                } elseif ($type == '4') {
                    Redis::setex('Shop:SalesMan:SmsMobile:' . $mobile, 600, $code);
                } elseif ($type == '5') {
                    Redis::setex('ShopWeb:SmsMobile:' . $mobile, 600, $code);
                }
                return '1';
            }
        } catch(Exception $e) {
            Log::INFO("手机发送短信错误 ". $mobile);
            Log::INFO('type: ' . $type . " ,mode: " . $mode);
            return '1';
        }
    }
    //验证验证码
    public static function loginIdentifyingCodeVerify($mobile,$code,$type) {
        if(!$mobile || !$code) {
            return false;
        }
        if($type == '1') {
            if($code == Redis::get('User:SmsMobile:'.$mobile)) {
                Redis::del('User:SmsMobile:'.$mobile);
                return true;
            }
        } elseif($type == '2') {
            if($code == Redis::get('Shop:SmsMobile:'.$mobile)) {
                Redis::del('Shop:SmsMobile:'.$mobile);
                return true;
            }
        } elseif($type == '3') {
            if($code == Redis::get('Admin:SmsMobile:'.$mobile)) {
                Redis::del('Admin:SmsMobile:'.$mobile);
                return true;
            }
        } elseif($type == '4') {
            if($code == Redis::get('Shop:SalesMan:SmsMobile:'.$mobile)) {
                Redis::del('Shop:SalesMan:SmsMobile:'.$mobile);
                return true;
            }
        } elseif($type == '5') {
            if($code == Redis::get('ShopWeb:SmsMobile:'.$mobile)) {
                Redis::del('ShopWeb:SmsMobile:'.$mobile);
                return true;
            }
        }
        return false;
    }

    public static function collectMoneyToShop($mobile,$money) {
        try {
            if (env('WEB_URL') != 'https://www.fbw-china.com/') {
                return true;
            }
            $c = new TopClient;
            $c->appkey = env('ALIDAYU_APPKEY');
            $c->secretKey = env('ALIDAYU_SECRETKEY');
            $req = new AlibabaAliqinFcSmsNumSendRequest;
            $req->setExtend("123456");
            $req->setSmsType("normal");
            $req->setSmsFreeSignName("翻贝网");
            $req->setSmsParam("{\"money\":\"" . $money . "\"}");
            $req->setRecNum($mobile);
            $req->setSmsTemplateCode("SMS_34870048");
            $resp = $c->execute($req);
            return true;
        } catch(Exception $e) {
            return false;
        }
    }
    //mode 0表示第一入住，1表示续约，bankcard只提取后四位
    public static function firstRunToShop($mobile,$shopname,$cardholder,$bankcard,$mode = 0) {
        try {
            //正式环境或手机号码是邓志强的手机号码
            if ((env('WEB_URL') != 'https://www.fbw-china.com/') && ($mobile != '15070858455')) {
                return true;
            }
            $c = new TopClient;
            $c->appkey = env('ALIDAYU_APPKEY');
            $c->secretKey = env('ALIDAYU_SECRETKEY');
            $req = new AlibabaAliqinFcSmsNumSendRequest;
            $req->setExtend("123456");
            $req->setSmsType("normal");
            $req->setSmsFreeSignName("翻贝网");
            $req->setSmsParam("{\"shopname\":\"" . $shopname . "\",\"cardholder\":\"" . $cardholder . "\",\"bankcard\":\"" . $bankcard . "\"}");
            $req->setRecNum($mobile);
            if($mode == 0)
            {
                $req->setSmsTemplateCode("SMS_64535035");
            }
            else
            {
                $req->setSmsTemplateCode("SMS_64580023");
            }
            $resp = $c->execute($req);
            return true;
        } catch(Exception $e) {
            return false;
        }
    }

    public static function accountToShop($mobile,$shopname,$money,$banknumber) {
        try {
            if (env('WEB_URL') != 'https://www.fbw-china.com/') {
                return true;
            }
            $c = new TopClient;
            $c->appkey = env('ALIDAYU_APPKEY');
            $c->secretKey = env('ALIDAYU_SECRETKEY');
            $req = new AlibabaAliqinFcSmsNumSendRequest;
            $req->setExtend("123456");
            $req->setSmsType("normal");
            $req->setSmsFreeSignName("翻贝网");
            $req->setSmsParam("{\"money\":\"" . $money . "\",\"shopname\":\"" . $shopname . "\",\"banknumber\":\"" . $banknumber . "\"}");
            $req->setRecNum($mobile);
            $req->setSmsTemplateCode("SMS_48320121");
            $resp = $c->execute($req);
            return true;
        } catch(Exception $e) {
            return false;
        }
    }
}